# -*- encoding=utf8 -*-

__author__ = "code哥"
import asyncio
import json
from sanic.blueprints import Blueprint
from sanic_openapi import doc
from conn_pool import get_redis, REDIS_APP_EXECUTION_CURRENT_INFO

rs = get_redis()

blueprint = Blueprint(__name__, '/api')


@doc.summary("websocket 推送当前执行信息")
async def feed(request, ws):
    while True:
        msg = rs.get(REDIS_APP_EXECUTION_CURRENT_INFO)
        if msg is not None and len(str(msg)) > 0:
            msg_json = json.loads(msg, encoding='utf-8')
            resp = json.dumps(msg_json, ensure_ascii=False)
            print('send resp: ', resp)
            await ws.send(resp)
        await asyncio.sleep(5)


blueprint.add_websocket_route(feed, '/curr_exec_ws')
